package com.example.isoftservice.service.impl;


import com.example.isoftservice.mapper.DealerMapper;
import com.example.isoftservice.pojos.Dealer;
import com.example.isoftservice.service.DealerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassNmae:DealerServiceImpl
 * Descriptiopn:
 *
 * @Create 2023/10/26 11:11
 * @Author yidao
 */
@Service
public class DealerServiceImpl implements DealerService {
    @Resource
    DealerMapper dealerMapper;


    //查询经销商信息
    @Override
    @Cacheable(cacheNames ="dealer",key="#dealer")
    public Map<String, Object> searchDealerInfo(Dealer dealer) {
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(dealer.getPage(),dealer.getRows());
        List<Map<String, Object>> list =dealerMapper.select(dealer);
        PageInfo<Map<String,Object>> pageInfo= new PageInfo<>(list);
        //获取分页后的信息
        map.put("code",0);
        map.put("meaaage","成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());

        return map;

    }
    @Cacheable(cacheNames ="dealer",key="#dealer")
    //查询经销商类别信息
    @Override
    public Map<String, Object> searchDealerTypeInfo() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list =dealerMapper.selectType();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",list);
        return map;
    }

    //查询机构大类信息
    @Override
    @Cacheable(cacheNames ="instiutionbig",key="#dealer")
    public Map<String, Object> searchInstitutionBigInfo() {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list =dealerMapper.selectInstitutionBigCategoryName();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",list);
        return map;
    }

    @Override
    @Cacheable(cacheNames ="instiutionsmall",key="#dealer")
    public Map<String, Object> searchInstitutionSmallByBig(Dealer dealer) {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list =dealerMapper.selectInstitutionSmallByBig(dealer);
        map.put("code",0);
        map.put("message","成功");
        map.put("data",list);
        return map;
    }

    //删除经销商信息
    @Override
    @CacheEvict(cacheNames = "dealer" ,allEntries = true)
    public Map<String, Object> removeDealerById(Dealer dealer) {
        Map<String,Object> map =new HashMap<>();

        Integer count=dealerMapper.delete(dealer);
        if(count >=1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","删除成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","删除失败");
        }
        return map;
    }

    //添加经销商信息
    @Override
    @CacheEvict(cacheNames = "dealer" ,allEntries = true)
    public Map<String, Object> addDealerInfo(Dealer dealer) {
        Map<String,Object> map =new HashMap<>();

        Integer count=this.dealerMapper.insert(dealer);
        if(count ==1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","添加成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","添加失败");
        }
        return map;
    }

    //修改经销商信息
    @Override
    @CacheEvict(cacheNames = "dealer" ,allEntries = true)
    public Map<String, Object> modifyDealerById(Dealer dealer) {
        Map<String,Object> map =new HashMap<>();

        Integer count=dealerMapper.update(dealer);
        if(count >=1){
            map.put("flag",0);
            map.put("code",true);
            map.put("message","更新成功");
        }else{
            map.put("flag",1);
            map.put("code",false);
            map.put("message","更新失败");
        }
        return map;
    }

}
